﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using LPV_POS.Module;
using LPV_POS.mh_pos.mh_ban_hang;
//using LPV_POS.In;

namespace LPV_POS.mh_pos
{
    public partial class mh_banhang8_Tinh_Tien_HOTELGio : DevExpress.XtraEditors.XtraForm
    {
        #region "   Khai bao DataTable"
        DataTable tbBH_PHIEU_TMP = null;
        DataTable tbBH_PHIEU_CT_TMP = null;
        DataTable tbDonGia = null;
        #endregion

        #region "   mh_banhang8_Tinh_Tien_HOTELGio"
        public mh_banhang8_Tinh_Tien_HOTELGio(DataTable _tbBH_PHIEU_TMP, DataTable _tbBH_PHIEU_CT_TMP)
        {
            InitializeComponent();
            tbBH_PHIEU_TMP = _tbBH_PHIEU_TMP;
            tbBH_PHIEU_CT_TMP = _tbBH_PHIEU_CT_TMP;

            this.SetgrdDonGia_DataSource();
            txtGhiChu.Text = _tbBH_PHIEU_TMP.Rows[0]["GHI_CHU"] + "";

            this.Load_DonGia(tbBH_PHIEU_TMP.Rows[0]["NGAY_GIO_VAO"]);
        }

        #region "   Load Don Gia"
        void Load_DonGia(object dNgayGioVao)
        {
            tbDonGia = new thuvien().SP_SelectDataTable("sp_DM_TIENGIO2",
                                                            new string[] { "@flag", "@DM_BAN_MA", "@TU_GIO" },
                                                            new object[] { 7, tbBH_PHIEU_TMP.Rows[0]["DM_BAN_MA"], dNgayGioVao});
            //cboDonGia.DataSource = tbDonGia;
            grdDonGia.DataSource = tbDonGia;
        }
            #endregion

        #endregion

        #region "   SetgrdDonGia_DataSource"
        void SetgrdDonGia_DataSource()
        {
            DataTable tb = new DataTable();
            try
            {
                DataTable tbDG = tbBH_PHIEU_CT_TMP.Copy();
                for (int i = 0; i < tbDG.Rows.Count; i++)
                {
                    if (string.Concat(tbDG.Rows[i]["MAHH"], "").IndexOf("Tien_Gio") < 0)
                    {
                        tbDG.Rows.RemoveAt(i);
                        i--;
                    }
                }
                for (int i = 0; i < tbDG.Rows.Count; i++)
                    tbDG.Rows[i]["STT"] = i + 1;
                grdTienPhong.DataSource = tbDG;
            }
            catch (Exception ex)
            {
                string s = ex.Message;
            }
        }
        #endregion
        Boolean iLoadForm = false;
        #region "   mh_banhang8_Tinh_Tien_HOTELGio_Load"
        private void mh_banhang8_Tinh_Tien_HOTELGio_Load(object sender, EventArgs e)
        {
            this.ShowInfo();
            if (chkKaraokeAuTo.Checked)
                this.btnTinhTienGio_Click(null, null);
            iLoadForm = true;
        }
        #endregion

        #region "   btnThoat_Click"
        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Dispose();
        }
        #endregion

        #region "   btnThucHien_Click"
        private void btnThucHien_Click(object sender, EventArgs e)
        {//Luu Update
            try
            {
                //BH_PHIEU_TMP
                tbBH_PHIEU_TMP.Rows[0]["GHI_CHU"] = txtGhiChu.Text.Trim();
                tbBH_PHIEU_TMP.Rows[0]["NGAY_GIO_VAO"] = dtpVao.Value;
                tbBH_PHIEU_TMP.Rows[0]["NGAY_GIO_RA"] = dtpRa.Value;

                //BH_PHIEU_CT_TMP
                DataTable tb = (DataTable)grdTienPhong.DataSource;
                tbBH_PHIEU_CT_TMP.Columns["THANH_TIEN"].Expression = "";
                tbBH_PHIEU_CT_TMP.Columns["THANH_TIEN"].ReadOnly = false;
                tb.Columns["THANH_TIEN"].Expression = "";
                tb.Columns["THANH_TIEN"].ReadOnly = false;

                tbBH_PHIEU_CT_TMP.Columns["GIA_BAN"].Expression = "";
                tbBH_PHIEU_CT_TMP.Columns["GIA_BAN"].ReadOnly = false;
                tb.Columns["GIA_BAN"].Expression = "";
                tb.Columns["GIA_BAN"].ReadOnly = false;

                int igio = 0;
                float iSL = 0;
                int iDG = 0;
                double iGiamGia = 0;
                for (int i = 0; i < tbBH_PHIEU_CT_TMP.Rows.Count; i++)
                {
                    #region "   da ton tai if (tbBH_PHIEU_CT_TMP.Rows[i]["MAHH"] + "" == "Tien_Gio")"
                    if (tbBH_PHIEU_CT_TMP.Rows[i]["MAHH"].ToString().ToLower().IndexOf("tien_gio") >= 0)
                    {
                        igio++;
                        if (tb.Select("STT=" + igio).Length > 0)
                        {
                            DataRow drTMP = tbBH_PHIEU_CT_TMP.Rows[i];
                            drTMP.ItemArray = tb.Select("STT=" + igio)[0].ItemArray;
                            //drTMP["SO_LUONG"] = 1;
                            #region "   Giam tien_gio"
                            //
                            float.TryParse(tb.Rows[i]["SO_LUONG"] + "", out iSL);
                            int.TryParse(tb.Rows[i]["DON_GIA"] + "", out iDG);

                            object iTT = iSL * iDG;
                            iGiamGia = this.tinh_phantram(iTT + "", nudGiam_PhanTram.Value.ToString().Replace(",", ""));
                            //
                            drTMP["CHIETKHAU_PHANTRAM"] = nudGiam_PhanTram.Value;
                            drTMP["CHIETKHAU_TIEN"] = iGiamGia;
                            #endregion
                        }
                        else
                        {
                            //xoa du lieu
                            Int64 iobjid = 0;
                            Int64.TryParse(tbBH_PHIEU_CT_TMP.Rows[i]["OBJID"] + "", out iobjid);
                            if (iobjid > 0)
                            {
                                string sSQL = "DELETE FROM BH_PHIEU_CT_TMP WHERE OBJID = " + iobjid;
                                new thuvien().SP_Update("SP_EXECUTESQL", new string[] { "@sSQL" }, new object[] { sSQL });
                            }
                            tbBH_PHIEU_CT_TMP.Rows.RemoveAt(i--);
                        }
                    }
                    #endregion
                }
                if (igio < tb.Rows.Count)
                {
                    for (int i = igio + 1; i <= tb.Rows.Count; i++)
                    {
                        DataRow drTMP = tbBH_PHIEU_CT_TMP.NewRow();
                        drTMP.ItemArray = tb.Select("STT=" + i)[0].ItemArray;
                        //drTMP["SO_LUONG"] = 1;//99999999999999
                        #region "   Giam tien_gio"
                        //
                        float.TryParse(tb.Rows[i-1]["SO_LUONG"] + "", out iSL);
                        int.TryParse(tb.Rows[i-1]["DON_GIA"] + "", out iDG);

                        decimal dPhanTram = nudGiam_PhanTram.Value;

                        object iTT = iSL * iDG;
                        iGiamGia = this.tinh_phantram(iTT + "", dPhanTram.ToString().Replace(",", ""));
                        //
                        drTMP["CHIETKHAU_PHANTRAM"] = nudGiam_PhanTram.Value;
                        drTMP["CHIETKHAU_TIEN"] = iGiamGia;
                        #endregion
                        tbBH_PHIEU_CT_TMP.Rows.Add(drTMP);
                    }
                }
                tbBH_PHIEU_CT_TMP.Columns["THANH_TIEN"].Expression = "SO_LUONG * DON_GIA - CHIETKHAU_TIEN";
                tb.Columns["THANH_TIEN"].Expression = "SO_LUONG * DON_GIA - CHIETKHAU_TIEN";

                tbBH_PHIEU_CT_TMP.Columns["GIA_BAN"].Expression = "DON_GIA";
                tb.Columns["GIA_BAN"].Expression = "DON_GIA";

                //foreach (DataRow dr in tbBH_PHIEU_CT_TMP.Select("SO_LUONG=0"))
                for (int i = 0; i < tbBH_PHIEU_CT_TMP.Rows.Count; i++)
                {
                    if (tbBH_PHIEU_CT_TMP.Rows[i]["MAHH"].ToString().ToLower().IndexOf("tien_gio") >= 0)
                    {
                        if (tbBH_PHIEU_CT_TMP.Rows[i]["SO_LUONG"] + "" == "" || tbBH_PHIEU_CT_TMP.Rows[i]["SO_LUONG"] + "" == "0")
                        {
                            Int64 iobjid = 0;
                            Int64.TryParse(tbBH_PHIEU_CT_TMP.Rows[i]["OBJID"] + "", out iobjid);
                            if (iobjid > 0)
                            {
                                string sSQL = "DELETE FROM BH_PHIEU_CT_TMP WHERE OBJID = " + iobjid;
                                new thuvien().SP_Update("SP_EXECUTESQL", new string[] { "@sSQL" }, new object[] { sSQL });
                            }
                            tbBH_PHIEU_CT_TMP.Rows.RemoveAt(i--);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                string s = ex.Message;
                MessageBox.Show(s);
            }
            finally {
                tbBH_PHIEU_CT_TMP.Columns["THANH_TIEN"].Expression = "SO_LUONG * DON_GIA - CHIETKHAU_TIEN";
                tbBH_PHIEU_CT_TMP.Columns["GIA_BAN"].Expression = "DON_GIA";
            }
        }
        #endregion
        
        #region "   tinh_phantram"
        Double tinh_phantram(string _sThanhTien, string phantram)
        {//tinh cho ca CHIET_KHAU va VAT

            double _ThanhTien = double.Parse(_sThanhTien.Replace(",", ""));
            Double db = 0;
            try
            {
                db = Double.Parse("0" + phantram.Replace("%", ""));
                db = Math.Round(_ThanhTien * db / 100f / 100, 0) * 100;//lam tron 2 so (123,123.45 --> 123,100)
            }
            catch (Exception ex)
            {
                string s = ex.Message;
            }
            return db;
        }
        #endregion

        #region "   Get_Tien_by_GioThu"
        int Get_Tien_by_GioThu(int _iGioThu, ref int iLAM_TRON_GIO)
        {
            int iTien = 0;
            try
            {
                if (tbDonGia.Select("GIO_THU=" + _iGioThu).Length > 0)
                    int.TryParse(tbDonGia.Select("GIO_THU=" + _iGioThu)[0]["T2"] + "", out iTien);
                else if (tbDonGia.Rows.Count > 0)//giờ Thứ ... trở đi
                    int.TryParse(tbDonGia.Rows[tbDonGia.Rows.Count - 1]["T2"] + "", out iTien);
                //
                int.TryParse(tbDonGia.Rows[tbDonGia.Rows.Count - 1]["LAM_TRON_GIO"] + "", out iLAM_TRON_GIO);
                
            }
            catch (Exception ex)
            {
                MessageBox.Show("Thông báo\n" + ex.Message, "Get_Tien_by_GioThu(...)");
            }
            return iTien;
        }
        #endregion

        #region "   ShowForm"
        public DataTable ShowForm()
        {
            this.ShowDialog();
            return tbBH_PHIEU_CT_TMP;
        }
        #endregion

        #region "   ShowInfo"
        void ShowInfo()
        {
            string ichkKaraokeAuTo = new cpublic().Registry_Get("chkKaraokeAuTo");
            chkKaraokeAuTo.Checked = ichkKaraokeAuTo == "True";
            btnTinhTienGio.Enabled = !chkKaraokeAuTo.Checked;

            DateTime dNGAY_GIO_VAO;
            if (tbBH_PHIEU_TMP.Rows[0]["NGAY_GIO_VAO"] + "" == "")
                tbBH_PHIEU_TMP.Rows[0]["NGAY_GIO_VAO"] = Variable.ngay_gio_hienhanh;

            DateTime.TryParse(tbBH_PHIEU_TMP.Rows[0]["NGAY_GIO_VAO"] + "", out dNGAY_GIO_VAO);
            dtpVao.Value = dNGAY_GIO_VAO;
            //
            dtpRa.Value = Variable.ngay_gio_hienhanh;
            //
            decimal dGiam_PhanTram = 0;
            if (tbBH_PHIEU_CT_TMP.Select("MAHH LIKE 'Tien_Gio%'").Length > 0)
                decimal.TryParse(tbBH_PHIEU_CT_TMP.Select("MAHH LIKE 'Tien_Gio%'")[0]["CHIETKHAU_PHANTRAM"] + "", out dGiam_PhanTram);
            else
            {
                #region "   Giam Gia Default"
                cpublic ocpublic = new cpublic();
                string d1 = ocpublic.Registry_Get("dtpGiamTienGio_Tu");
                string d2 = ocpublic.Registry_Get("dtpGiamTienGio_Den");
                if (d1.Length == 8 && d2.Length == 8)
                {
                    string d3 = ocpublic.Registry_Get("nudGiam_TienGio_PhanTram");
                    //tu ngay
                    string l1 = d1.Substring(0, 4);
                    string l2 = d1.Substring(4, 2);
                    string l3 = d1.Substring(6, 2);
                    DateTime dngay1 = new DateTime(int.Parse(l1), int.Parse(l2), int.Parse(l3), 0, 0, 0);

                    //den ngay
                    l1 = d2.Substring(0, 4);
                    l2 = d2.Substring(4, 2);
                    l3 = d2.Substring(6, 2);
                    DateTime dngay2 = new DateTime(int.Parse(l1), int.Parse(l2), int.Parse(l3), 23, 59, 59);

                    if (dngay1 <= dtpVao.Value && dtpVao.Value <= dngay2)
                    {
                        decimal dc = 0;
                        decimal.TryParse(d3, out dc);
                        dGiam_PhanTram = dc;
                    }
                }
                #endregion
            }
            nudGiam_PhanTram.Value = dGiam_PhanTram;
        }
        #endregion

        #region "   dtpVao_ValueChanged"
        private void dtpVao_ValueChanged(object sender, EventArgs e)
        {
            if (iLoadForm)
                this.Load_DonGia(dtpVao.Value);
            string sT = "";
            //if (dtpVao.Value.DayOfWeek == System.DayOfWeek.Monday)
                sT = "T2";
            //else if (dtpVao.Value.DayOfWeek == System.DayOfWeek.Tuesday)
            //    sT = "T3";
            //else if (dtpVao.Value.DayOfWeek == System.DayOfWeek.Wednesday)
            //    sT = "T4";
            //else if (dtpVao.Value.DayOfWeek == System.DayOfWeek.Thursday)
            //    sT = "T5";
            //else if (dtpVao.Value.DayOfWeek == System.DayOfWeek.Friday)
            //    sT = "T6";
            //else if (dtpVao.Value.DayOfWeek == System.DayOfWeek.Saturday)
            //    sT = "T7";
            //else if (dtpVao.Value.DayOfWeek == System.DayOfWeek.Sunday)
            //    sT = "T1";

            lblNgay_Gio_Vao.Text = lblNgay_Gio_Vao.Tag + " (" + sT.Replace("T1", "CN") + ")";
            if (tbDonGia.Rows.Count > 0)
            {
                

                decimal dgia = 0;
                decimal.TryParse(tbDonGia.Rows[0][sT] + "", out dgia);
                if (tbDonGia.Rows[0]["TINH_GIA_USD"] + "" == "True")
                {
                    int iTI_GIA_USD = 0;
                    int.TryParse(Variable.tbBH_CAU_HINH.Rows[0]["TI_GIA_USD"] + "", out iTI_GIA_USD);
                    dgia = dgia * iTI_GIA_USD;
                }
                //nudDonGia.Value = dgia;
            }

            if (dtpVao.Value >= dtpRa.Value)
                nudTong_Gio.Value = 0;

            DataTable tbGio = new thuvien().SP_SelectDataTable("sp_TinhTienGio",
                                                        new string[] { "@flag", "@ban_ma", "@gio_vao", "@gio_ra"},
                                                        new object[] { 2, tbBH_PHIEU_TMP.Rows[0]["DM_BAN_MA"], dtpVao.Value, dtpRa.Value});
            decimal dgio = 0;
            if (tbGio.Rows.Count > 0)
            {
                decimal.TryParse(tbGio.Rows[0][0] + "", out dgio);
            }
            nudTong_Gio.Value = dgio;
            if (!iLoadForm) return;
            if (chkKaraokeAuTo.Checked)//if (grvTienPhong.RowCount == 0)
                this.btnTinhTienGio_Click(null, null);
        }
        #endregion

        #region "   nudTong_Gio_ValueChanged"
        private void nudTong_Gio_ValueChanged(object sender, EventArgs e)
        {
            //if (cboDonGia.SelectedItem + "" != "")
            {
                //decimal fDonGia = 0;
                //decimal.TryParse(cboDonGia.Text, out fDonGia);
                //DataRow dr = ((DataRowView)cboDonGia.SelectedItem).Row;
                //int iGIA_USD = 1;
                ////if (dr["TINH_GIA_USD"] + "" == "True")
                //int.TryParse(dr["TINH_GIA_USD"] + "", out iGIA_USD);

                //nudThanhTien.Value = fDonGia * nudTong_Gio.Value * iGIA_USD;
            }
        }
        #endregion

        #region "   btnTinhTienGio_Click"
        private void btnTinhTienGio_Click(object sender, EventArgs e)
        {
            //BH_PHIEU_CT_TMP
            DataTable tbBH_PHIEU_CT_TMP2011 = (DataTable)grdTienPhong.DataSource;

            decimal iGio = 1;
            decimal iGio_Cong = 1;
            
            int iRow = 0;
            while (iGio <= nudTong_Gio.Value+0.9M)
            {
                DataRow dr = null;
                iRow = 0;
                for (int i = 0; i < tbBH_PHIEU_CT_TMP2011.Rows.Count; i++)
                {
                    if (tbBH_PHIEU_CT_TMP2011.Rows[i]["MAHH"].ToString().ToLower().IndexOf("tien_gio") >= 0)
                    {
                        if (++iRow == iGio)
                        {
                            dr = tbBH_PHIEU_CT_TMP2011.Rows[i];
                            break;
                        }
                    }
                }
                int iLAM_TRON_GIO=0;//bao nhieu phut thi tinh la 1 gio
                int iDON_GIA = this.Get_Tien_by_GioThu((int)iGio, ref iLAM_TRON_GIO);
                if (iDON_GIA > 0)
                {
                    if (dr == null)
                    {// neu ko co dong nao THOA DK
                        dr = tbBH_PHIEU_CT_TMP2011.NewRow();
                        dr["MAHH"] = "Tien_Gio";
                        tbBH_PHIEU_CT_TMP2011.Rows.Add(dr);
                        dr["CHIETKHAU_TIEN"] = 0;
                        dr["KM_TIEN"] = 0;
                        dr["VAT_TIEN"] = 0;
                        dr["GHI_CHU"] = "";
                    }
                    dr["STT"] = iGio;
                    dr["OBJNAME_HH_ROOT"] = Variable.tbDM_LOAIDICHVU.Select("OBJID=2")[0]["TIEN_PHONG_GIO"];
                    dr["OBJNAME_HH"] = Variable.tbDM_LOAIDICHVU.Select("OBJID=2")[0]["TIEN_PHONG_GIO"];
                    if (iGio < nudTong_Gio.Value)
                        dr["SO_LUONG"] = 1;
                    else
                    {
                        if (iLAM_TRON_GIO > 0)
                        {
                            //neu so phut > phut quy dinh (bao nhieu phut thi dc 1h)
                            if ((nudTong_Gio.Value - iGio + 1) * 60 > iLAM_TRON_GIO)
                                dr["SO_LUONG"] = 1;//1h
                            else
                                dr["SO_LUONG"] = 0;
                        }
                        else
                            dr["SO_LUONG"] = nudTong_Gio.Value - iGio + 1;

                    }

                    dr["DON_GIA"] = iDON_GIA;

                    dr["NGAY_TINH_GIO"] = dtpRa.Value;
                    iGio_Cong++;
                }
                iGio++;
            }
            
            //if (iRow > --iGio_Cong)
            //{

            //}
            //else 
            if (--iGio_Cong < tbBH_PHIEU_CT_TMP2011.Rows.Count)
            {
                try
                {
                    for (int i = (int)iGio_Cong + 1; i <= tbBH_PHIEU_CT_TMP2011.Rows.Count; i++)
                    {
                        tbBH_PHIEU_CT_TMP2011.Rows.RemoveAt(--i);
                    }
                }
                catch (Exception ex)
                {
                    string s = ex.Message;
                }
            }
            try
            {
                for (int i = 0; i < tbBH_PHIEU_CT_TMP2011.Select("DON_GIA=0").Length; i++)
                {
                    tbBH_PHIEU_CT_TMP2011.Rows.Remove(tbBH_PHIEU_CT_TMP2011.Select("DON_GIA=0")[i--]);
                }
            }
            catch (Exception ex)
            {
                string s = ex.Message;
            }
            grdTienPhong.DataSource = tbBH_PHIEU_CT_TMP2011;
        }
        #endregion

        private void btntonggio_Click(object sender, EventArgs e)
        {
            try
            {
                mh_so omh_so = new mh_so("Tổng giờ", (int)nudTong_Gio.Value);
                decimal igio = omh_so.ShowFormD();
                if (omh_so.DialogResult == DialogResult.OK)
                    nudTong_Gio.Value = igio;
            }
            catch (Exception ex)
            {
                MessageBox_LPV.Show("Thông báo\n" + ex.Message);
            }
        }

        private void btnghichu_Click(object sender, EventArgs e)
        {
            mh_chu omh_chu = new mh_chu();
             string str= omh_chu.ShowForm("Ghi chú", txtGhiChu.Text, 300, 300, false);
             if (omh_chu.DialogResult == DialogResult.OK)
                 txtGhiChu.Text = str;
        }

        private void chkKaraokeAuTo_CheckedChanged(object sender, EventArgs e)
        {
            btnTinhTienGio.Enabled = !chkKaraokeAuTo.Checked;
            new cpublic().Registry_Set("chkKaraokeAuTo", chkKaraokeAuTo.Checked);
        }

        private void btnCauHinh_Click(object sender, EventArgs e)
        {
            mh_banhang8_Giam_TienGio frm = new mh_banhang8_Giam_TienGio();
            frm.ShowDialog();
        }

    }
}